'''
Created on 27/02/2009

@author: eh01
'''
from dao.nodeDAO import NodeDAO
from domain.node import Node
from exception.validationexception import ValidationException
from google.appengine.ext import db
import datetime

class Message(db.Model):

    nodeId = db.IntegerProperty()
    timestamp = db.DateTimeProperty(auto_now=True)
    contentType = db.StringProperty()
    body = db.TextProperty()
    
    def validate(self):
        if not self.is_node_currently_connected():
            raise ValidationException("Node with node id = " + str(self.nodeId) + " is not currently connected")
        
    def is_node_currently_connected(self):
        thirty_mins = datetime.timedelta(minutes=30)
        date_time_now = datetime.datetime.now()
        nodeDAO = NodeDAO()
        node = nodeDAO.findByNodeId(str(self.nodeId))
        if node:
            if date_time_now - node.timestamp < thirty_mins:
                return True
        return False